<template>
  <el-form
    ref="form"
    :model="form"
    label-width="120px"
    :rules="formRules"
    v-loading="locking"
  >
    <el-form-item label="菜单名称" prop="name">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="菜单路径" prop="href">
      <el-input v-model="form.href"></el-input>
    </el-form-item>
    <el-form-item label="上级菜单" prop="parentId">
      <el-select v-model="form.parentId" placeholder="请选择上级菜单">
        <el-option label="无上级菜单" :value="-1" />
        <el-option
          v-for="item of parentMenus"
          :label="item.name"
          :key="item.id"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <el-input v-model="form.description"></el-input>
    </el-form-item>
    <el-form-item label="前端图标" prop="icon">
      <el-input v-model="form.icon"></el-input>
    </el-form-item>
    <el-form-item label="是否显示" prop="shown">
      <el-radio-group v-model="form.shown">
        <el-radio :label="true">显示</el-radio>
        <el-radio :label="false">隐藏</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="排序" prop="orderNum">
      <el-input-number
        v-model="form.orderNum"
        type="number"
        :recision="0"
      ></el-input-number>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        @click="onSubmit"
        >{{ isEdit ? "修改" : "创建" }}</el-button
      >
      <el-button type="primary" @click="reset">重 置</el-button>
    </el-form-item>
  </el-form>
</template>
<script lang="ts">
import { createOrUpdate, getEditMenuInfo } from '@/services/menu'
import { wrapAsyncWithLoading } from '@/utils/helper'
import { Form } from 'element-ui'
import Vue from 'vue'
/**
 * public props: id
 * public events: suc,error
 * public methods: reset
 */
export default Vue.extend({
  name: 'menu-create-or-update',
  props: ['id'],
  // type: Number | String// vue types
  data () {
    return {
      form: {
        id: null, // id菜单ID，更新时须带上传给后台 false
        parentId: -1,
        name: '',
        href: '',
        icon: '',
        orderNum: 1,
        description: '',
        shown: false
      },
      formRules: {
        parentId: [{ required: true }],
        name: [{ required: true }],
        href: [{ required: true }],
        icon: [],
        orderNum: [],
        description: [],
        shown: []
      },
      parentMenus: [],
      locking: false
    }
  },
  computed: {
    isEdit (): boolean {
      return this.id > 0
    }
  },
  created () {
    // todo 用了props 就提示
    wrapAsyncWithLoading(this, 'locking', 'onSubmit')
    wrapAsyncWithLoading(this, 'locking', 'loadMenuInfo')

    this.loadMenuInfo()
  },
  methods: {
    async loadMenuInfo () {
      const { data } = await getEditMenuInfo(this.id || -1)
      if (data.code === '000000') {
        this.parentMenus = data.data.parentMenuList
        if (this.isEdit) {
          this.form = data.data.menuInfo
        }
      }
    },
    async onSubmit () {
      const uiForm = this.$refs.form as Form
      try {
        await uiForm.validate()
      } catch (error) {
        return
      }
      try {
        await createOrUpdate(this.form)
        this.$emit('suc')
      } catch (error) {
        this.$emit('fail', error)
      }
    }
  }
})
</script>
